/*
 * BonusGenerator.cpp
 *
 *  Created on: Jun 13, 2012
 *      Author: gonzalo
 */

#include "BonusGenerator.h"
#include <time.h>
#include <string>
#include <list>
#include "widgets/bonus/BasicShieldBonus.h"
#include "widgets/bonus/SuperShieldBonus.h"
#include "../common/utils/GameUtils.h"
#include "widgets/PlayScenarioPanel.h"
#include <unistd.h>
#include "../common/Constantes.h"
#include "widgets/bonus/MissileBonus.h"
#include "widgets/bonus/MinaBonus.h"
#include "widgets/bonus/InvisibleBonus.h"
#include "widgets/bonus/LaserBonus.h"

#define BASIC_SHIELD_PROBABILITY 0.2f
#define SUPER_SHIELD_PROBABILITY 0.2f
#define MISSILE_PROBABILITY 0.2f
#define MINA_PROBABILITY 0.2f
#define LASER_PROBABILITY 0.1f
#define INVISIBLE_PROBABILITY 0.1f

#define NOTHING_PROBABILITY 0.2f
#define BONUS_PROBABILITY 0.8f

Bonus* BonusGenerator::generateBonus(Asteroide* deadAsteroide,Widget* widget){

	std::list< std::pair<BonusType,float>* >* probability;
	probability = new std::list< std::pair<BonusType,float>* >();
	probability->push_back( new std::pair<BonusType,float>(BASIC_SHIELD,BASIC_SHIELD_PROBABILITY) );
	probability->push_back( new std::pair<BonusType,float>(SUPER_SHIELD,SUPER_SHIELD_PROBABILITY) );
	probability->push_back( new std::pair<BonusType,float>(MISSILE,MISSILE_PROBABILITY) );
	probability->push_back( new std::pair<BonusType,float>(MINA,MINA_PROBABILITY) );
	probability->push_back( new std::pair<BonusType,float>(LASER,LASER_PROBABILITY) );
	probability->push_back( new std::pair<BonusType,float>(INVISIBLE,INVISIBLE_PROBABILITY) );

	//Get a random value to check if im gonna create a bonus
	float random = GameUtils::getRandFloatValue();

	Bonus* bonusGenerated = NULL;
	if(random > NOTHING_PROBABILITY) {
		//Get a new random
		random = GameUtils::getRandFloatValue();
		//Get the bonus using the probability
		BonusType bonusType = getBonusTypeByProbabilty(random,probability);

		//Generate the Bonus
		bonusGenerated = createBonus(bonusType,deadAsteroide,widget);
	}else{
		bonusGenerated = NULL;
	}

	return bonusGenerated;
}

Bonus* BonusGenerator::createBonus(BonusType bonus,Asteroide* deadAsteroide,Widget* widget){

	int x = deadAsteroide->getX();
	int y = deadAsteroide->getY();

	switch (bonus) {
		case BASIC_SHIELD:
				return new BasicShieldBonus(x,y,widget);
			break;
		case SUPER_SHIELD:
				return new SuperShieldBonus(x,y,widget);
			break;
		case MISSILE:
				return new MissileBonus(x,y,widget);
			break;
		case MINA:
				return new MinaBonus(x,y,widget);
			break;
		case LASER:
				return new LaserBonus(x,y,widget);
			break;
		case INVISIBLE:
				return new InvisibleBonus(x,y,widget);
			break;
		case COUNT:
		case BULLET:
		case USER_INFO:
				return NULL;
			break;
	}

	return NULL;
}

BonusType BonusGenerator::getBonusTypeByProbabilty(float value,std::list< std::pair<BonusType,float>* >* probability){
	float sum = 0;

	std::list< std::pair<BonusType,float>* >::iterator it;
	for (it = probability->begin(); it != probability->end(); ++it) {
		std::pair<BonusType,float>* pair =(std::pair<BonusType,float>*)*it;

		sum += pair->second ;
		if( sum >= value ){
			return pair->first;
		}
	}

	//Aca saldria y romperia .. no puede pasar tiene q estar arriba el numero
	return COUNT;

}
